package com.pass.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ClassName BitmapUtil
 * @Description Bitmap 工具类
 * @Author guchu
 * @Date 2021/1/3 16:38
 * @Version 1.0
 */
public class BitmapUtil {
    public static final String TAG = BitmapUtil.class.getSimpleName();

    public static byte[] bitmap2Byte(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if (bitmap != null) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] bitmapByte = stream.toByteArray();
            return bitmapByte;
        }
        return null;
    }

    public static Bitmap byte2Bitmap(byte[] bitmapByte) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length);
        return bitmap;
    }

    /**
     * @param bitmap
     * @param rotate  顺时针旋转
     * @param mirrorX X轴 镜像
     * @return
     */
    public static Bitmap rotateBitmap(Bitmap bitmap, int rotate, boolean mirrorX) {
        Matrix matrix = new Matrix();
        matrix.setRotate(rotate);
        if (mirrorX) {
            matrix.postScale(-1f, 1f);
        }
        Bitmap rotateBitmap = null;
        if (bitmap != null) {
            rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
            bitmap.recycle(); // 回收旧Bitmap
        }
        return rotateBitmap;
    }

    public static boolean saveBitmap2SD(Bitmap bitmap, String filePath, boolean deleteIfExists) throws IOException {
        File savedFile = new File(filePath);
        if (savedFile.exists()) {
            if (!deleteIfExists) {
                Log.d(TAG, String.format("%s already exists", filePath));
                return false;
            }
            savedFile.delete();
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        FileOutputStream fileOutputStream = new FileOutputStream(savedFile);
        fileOutputStream.write(baos.toByteArray());
        fileOutputStream.close();
        return true;
    }

    public static Bitmap getBitmapFromSD(String filePath) throws IOException {
        File bitmapFile = new File(filePath);
        if (!bitmapFile.exists()) {
            throw new FileNotFoundException(filePath);
        }
        return BitmapFactory.decodeFile(filePath);
    }

    /**
     * 获取图片旋转的角度
     *
     * @param path 照片路径
     * @return 角度
     */
    public static int getRotatedDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    public static Drawable bitmap2Drawable(Context context, Bitmap bitmap) {
        return new BitmapDrawable(context.getResources(), bitmap);
    }

    public static Bitmap drawable2Bitmap(BitmapDrawable drawable) {
        return drawable.getBitmap();
    }

}
